<template>
  <div class="mim">
    <span class="fd" @click="pay()">完成</span>
    <div class="fh">
      <van-nav-bar
        title="请输入密码"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div class="kuang">
      <van-password-input
      :value="value"
      info="密码为 6 位数字"
      :focused="showKeyboard"
      @focus="showKeyboard = true"
    />

    <!-- 数字键盘 -->
    <van-number-keyboard
      :show="showKeyboard"
      @input="onInput"
      @delete="onDelete"
      @blur="showKeyboard = true"
    />
    </div>

  </div>
</template>
<script>
import {getorders} from '../api/order'
import { getaddress } from '../api/address'
export default {
  data() {
    return {
      value: '',
      showKeyboard: true,
      list:[],
      lists:[],
      listss:[],
      id: ''
    }
  },
  created(){
      if(localStorage.getItem('selectedCart')){
      this.list = JSON.parse(localStorage.getItem('selectedCart'))
      //计算总价
      this.list.forEach(item=>{
        // this.sum += (item.product.price*item.quantity);
        this.lists.push(
          {
            quantity: item.quantity,
            product: item.product._id,
            price: item.product.price})
      })
      console.log(this.lists)
    }
  },

  methods: {
    onInput(key) {
      this.value = (this.value + key).slice(0, 6);
    },
    onDelete() {
      this.value = this.value.slice(0, this.value.length - 1);
    },
    onClickLeft() {

      this.$router.push({name:'Order',query:{id:this.id}});
    },
    async pay() {
      this.id = this.$route.query.id;
      const res = await getaddress(this.id)
      this.listss = res.data;
      console.log(this.listss);
    if(this.value==123456){
      console.log(this.value);
      const orderDate = {
        receiver: this.listss.receiver,
        regions: this.listss.regions,
        address: this.listss.address,
        orderDetails:
          this.lists
        }
      const order = await getorders(orderDate);
      console.log(order)
        localStorage.removeItem('selectedCart')
        this.$router.push({name:'ChengGong',query:{id:this.id}});
    }else{
      this.$router.push({name:'ShiBai'});
    }
  },
  }
}
</script>
<style scoped>
.mim{
  width: 100%;
  position: relative;
}
.kuang{
  margin-top: 200px;
}
p{
  text-align: center;
  font-size: 20px;
}
.fd{
  position: absolute;
  left: 0;
  bottom: 0;
  z-index: 9000;
  display: block;
  width: 125px;
  height: 54px;
  text-align: center;
  line-height: 54px;
}

</style>
